﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0231", Name = "日期各子项目检证", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0231 : VerfityOneResult
    {
        [Category("元号项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck GannNennItemChecked { get; set; }

        [Category("年项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck YearItemChecked { get; set; }

        [Category("月项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck MonthItemChecked { get; set; }

        [Category("日项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck DayItemChecked { get; set; }

        [Category("文字栏项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck TextItemChecked { get; set; }

        [Category("入退院项目检查标志"), Description(" 忽略,存在,不存在")]
        public EnumEntryItemCheck HospitalItemChecked { get; set; }

       　

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = CheckEachResult(result);
     
            if (bresult)
            {
                System.Console.WriteLine("VerfityRQ0231");
                System.Console.WriteLine(Name);
            }

            return bresult;
        }
            

        private bool  CheckEachResult(List<List<string>> result)
        {
            List<string> resultList =result.SelectMany(o => o).ToList();
          
          

            bool ret = true;
            ret = GannNennItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "gg", GannNennItemChecked);
            if (ret == false)
            {
                return false;
            }
            ret = YearItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "yy", YearItemChecked);
            if (ret == false)
            {
                ret = YearItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "yyyy", YearItemChecked);
                if (ret == false)
                    return false;
            }
            ret = MonthItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "MM", MonthItemChecked);
            if (ret == false)
            {
                return false;
            }
            ret = DayItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "dd", DayItemChecked);
            if (ret == false)
            {
                return false;
            }
            ret = TextItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "tx", TextItemChecked);
            if (ret == false)
            {
                return false;
            }
            ret = HospitalItemChecked == EnumEntryItemCheck.忽略 || CheckItemValue(resultList, "hp", HospitalItemChecked);
            if (ret == false)
            {
                return false;
            }

            return true ;
        }

        private bool CheckItemValue(IEnumerable<string> entryResultList, string item, EnumEntryItemCheck itemChecked)
        {
            //if (itemChecked == EnumEntryItemCheck.不存在)
            //{
                return entryResultList.Any(o => CheckCol(o, item, itemChecked));
            //}
            //else if (itemChecked == EnumEntryItemCheck.存在)
            //{
            //    return entryResultList.All(o => { return checkCol(o, item, itemChecked); });
            //}
            //else
            //{
            //    return false;
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entryResult">单列录入结果 </param>
        /// <param name="item">需要检查的项目</param>
        /// <param name="itemChecked"> </param>
        private bool CheckCol(string entryResult,string item,EnumEntryItemCheck itemChecked)
        {
            if(string.IsNullOrEmpty(entryResult.Replace("/",string.Empty)))
            {
                return false ;
            }

            if (string.IsNullOrEmpty(entryResult))
            {
                return false;
            }

            List<string> dateFormatParts = ChildImageObject.CategorySetting.DataFormat.Split(new char[] { '/' }).ToList();
            //找到项目位置
            int itemIndex = dateFormatParts.IndexOf(item);
            if (itemIndex == -1)
            {
                return false;
            }

            string[] splitedResult = entryResult.Split(new char[]{'/'});
            string itemValue = splitedResult.Count() > itemIndex ? splitedResult[itemIndex] : string.Empty;

            switch (itemChecked)
            {
                case EnumEntryItemCheck.忽略:
                    return false;
                case EnumEntryItemCheck.不存在:
                    //return splitedResult.Any(o => string.IsNullOrEmpty(o)) ? string.IsNullOrEmpty(itemValue) : false;
                    return string.IsNullOrEmpty(itemValue);
                case EnumEntryItemCheck.存在:
                    return  /*item=="tx" ? checkText(itemValue) :*/  !string.IsNullOrEmpty(itemValue);
                default :
                    return false;
            }            
        }

        //private bool checkText(string itemValue)
        //{ 
        //    return this.Text == string.Empty ?
        //        !string.IsNullOrEmpty(itemValue) :
        //        !string.IsNullOrEmpty(itemValue)  && itemValue.Equals(this.Text );
        //}

    }   
}
